<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Biometric_Model extends CI_Model {
	
	private $table= 'biometric_files';
	function __construct() {
		parent::__construct();
		$this->clientModel = $this->load->model('Client_Model'); 
	}
	
	function getFileList($api){
		$client = $this->clientModel->find($api);
		if($client !== false) {
			$this->db->select('b.*,f.first_name, f.last_name, f.middle_name');
			$this->db->from($this->table .' as b');
			$this->db->join('users as u','u.id=b.user_id','left');
			$this->db->join('file201 as f','f.user_id=u.id','left');
			$this->db->join('clients as c','c.id=u.client_id','left');
			$this->db->where('c.id', $client->id);
			$query = $this->db->get();
			return $query->result();
		}
		
		return false;
		
	}
	
	function saveOrUpdate($api,$params){
		$table = 'biometric_files';
		$client = $this->clientModel->find($api);
		if($client !== false) {
			if(isset($params['id'])){
				//update
				$id = $params['id'];
				unset($params['id']);
				$this->db->where("id", $id);
				$this->db->update($table, $params);
				return true;
			}else{
				$this->db->insert($table, $params);
			}
		}
		
	}
	
	function find($api,$id,$user_id){
		$client = $this->clientModel->find($api);
		if($client !== false) {
			$this->db->select('b.*,f.first_name, f.last_name, f.middle_name');
			$this->db->from($this->table .' as b');
			$this->db->join('users as u','u.id=b.user_id','left');
			$this->db->join('file201 as f','f.user_id=u.id','left');
			$this->db->join('clients as c','c.id=u.client_id','left');
			$this->db->where('c.id', $client->id);
			$this->db->where('b.id', $id);
			$query = $this->db->get();
			return $query->result();
		}
		
		return false;
		
	}
	
}